package cn.findviewbydream.evil.main.controller;

import cn.findviewbydream.evil.main.entity.BaseMealTypeEntity;
import cn.findviewbydream.evil.main.entity.BaseTypeOfWorkEntity;
import cn.findviewbydream.evil.main.entity.MealEntity;
import cn.findviewbydream.evil.main.service.SettingMealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.util.List;

/********************************************
 * 工程名：MealOrder
 * 包名：cn.findviewbydream.evil.main.controller
 * 时间：16-12-22
 * 作者：白雨濃 evil
 * 邮箱：evilbai@foxmail.com
 * 内容：设置界面->菜品设置
 ********************************************/
@Controller
public class SettingMealController
{
    private final SettingMealService service;

    @Autowired
    public SettingMealController(SettingMealService settingMealService) {this.service = settingMealService;}

    @RequestMapping("/setting/meal")
    public String index(Model model)
    {
        model.addAttribute("mealTypeList", service.getBaseMealTypeList());
        return "setting/meal";
    }

    /**
     * 获得菜品类型列表
     */
    @RequestMapping("/setting/meal/get/type/list")
    @ResponseBody
    public List<BaseMealTypeEntity> getBaseMealTypeList()
    {
        return service.getBaseMealTypeList();
    }

    /**
     * 更改菜品类型状态
     */
    @RequestMapping(value = "/setting/meal/post/type/state", method = RequestMethod.POST)
    @ResponseBody
    public boolean postBaseMealTypeState(int id, boolean state)
    {
        return service.postBaseMealTypeState(id, state);
    }

    /**
     * 更改菜品类型的类型
     */
    @RequestMapping(value = "/setting/meal/post/type", method = RequestMethod.POST)
    @ResponseBody
    public boolean postBaseMealType(int id, String type)
    {
        return service.postBaseMealType(id, type);
    }

    /**
     * 插入新的菜品类型
     */
    @RequestMapping(value = "/setting/meal/post/type/new", method = RequestMethod.POST)
    @ResponseBody
    public boolean postNewBaseMealType(String type)
    {
        return service.postNewBaseMealType(type);
    }

    /**
     * 获得菜品列表
     */
    @RequestMapping("/setting/meal/get/list")
    @ResponseBody
    public List<MealEntity> getMealList(@RequestParam(required = false) Integer typeId)
    {
        return service.getMealList(typeId);
    }

    /**
     * 根据ID获得菜品
     */
    @RequestMapping("/setting/meal/get")
    @ResponseBody
    public MealEntity getMeal(int mealId)
    {
        return service.getMeal(mealId);
    }

    /**
     * 更改菜品状态（是否上市）
     */
    @RequestMapping(value = "/setting/meal/post/market", method = RequestMethod.POST)
    @ResponseBody
    public boolean postMealMarket(int mealId, boolean market)
    {
        return service.postMealMarket(mealId, market);
    }

    /**
     * 获得未关闭的菜品类型
     */
    @RequestMapping("/setting/meal/get/mealtype/list")
    @ResponseBody
    public List<BaseMealTypeEntity> getMealTypeList()
    {
        return service.getMealTypeList();
    }

    /**
     * 获得未关闭的工种
     */
    @RequestMapping("/setting/meal/get/cooktype/list")
    @ResponseBody
    public List<BaseTypeOfWorkEntity> getCookTypeList()
    {
        return service.getCookTypeList();
    }


    /**
     * 提交新的菜品(有图)
     */
    @RequestMapping(value = "/setting/meal/post/meal/new_img", method = RequestMethod.POST)
    @ResponseBody
    public String postNewMeal(String name,
                              @RequestParam(required = false) String synopsis,
                              Integer mealTypeId,
                              Integer typeOfWorkId,
                              Float price,
                              @RequestParam(required = false) Float cost,
                              @RequestParam(required = false) Integer mealMonthStart,
                              @RequestParam(required = false) Integer mealMonthEnd,
                              @RequestParam(required = false) CommonsMultipartFile img)
    {
        try
        {
            return service.postNewMeal(name, synopsis, mealTypeId, typeOfWorkId,
                    price, cost, mealMonthStart, mealMonthEnd, img) + "";
        }
        catch (Exception e)
        {
            return e.getMessage();
        }
    }

    /**
     * 提交新的菜品(无图)
     */
    @RequestMapping(value = "/setting/meal/post/meal/new", method = RequestMethod.POST)
    @ResponseBody
    public String postNewMeal(String name,
                              @RequestParam(required = false) String synopsis,
                              Integer mealTypeId,
                              Integer typeOfWorkId,
                              Float price,
                              @RequestParam(required = false) Float cost,
                              @RequestParam(required = false) Integer mealMonthStart,
                              @RequestParam(required = false) Integer mealMonthEnd)
    {
        try
        {
            return service.postNewMeal(name, synopsis, mealTypeId, typeOfWorkId,
                    price, cost, mealMonthStart, mealMonthEnd, null) + "";
        }
        catch (Exception e)
        {
            return e.getMessage();
        }
    }

    /**
     * 提交更新菜品(有图)
     */
    @RequestMapping(value = "/setting/meal/post/meal_img", method = RequestMethod.POST)
    @ResponseBody
    public String postMeal(int id,
                           String name,
                           @RequestParam(required = false) String synopsis,
                           Integer mealTypeId,
                           Integer typeOfWorkId,
                           Float price,
                           @RequestParam(required = false) Float cost,
                           @RequestParam(required = false) Integer mealMonthStart,
                           @RequestParam(required = false) Integer mealMonthEnd,
                           @RequestParam(required = false) CommonsMultipartFile img)
    {
        try
        {
            return service.postMeal(id, name, synopsis, mealTypeId, typeOfWorkId,
                    price, cost, mealMonthStart, mealMonthEnd, img) + "";
        }
        catch (Exception e)
        {
            return e.getMessage();
        }
    }

    /**
     * 提交更新菜品(无图)
     */
    @RequestMapping(value = "/setting/meal/post/meal", method = RequestMethod.POST)
    @ResponseBody
    public String postMeal(int id,
                           String name,
                           @RequestParam(required = false) String synopsis,
                           Integer mealTypeId,
                           Integer typeOfWorkId,
                           Float price,
                           @RequestParam(required = false) Float cost,
                           @RequestParam(required = false) Integer mealMonthStart,
                           @RequestParam(required = false) Integer mealMonthEnd)
    {
        try
        {
            return service.postMeal(id, name, synopsis, mealTypeId, typeOfWorkId,
                    price, cost, mealMonthStart, mealMonthEnd, null) + "";
        }
        catch (Exception e)
        {
            return e.getMessage();
        }
    }
}
